package org.puc.enquete.entity;

import java.io.Serializable;
import java.util.List;
import javax.persistence.Basic;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.OneToMany;
import javax.persistence.Table;

@Entity
@Table(name = "OPCAO")
@NamedQueries({
    @NamedQuery(name = "Opcao.findAll", query = "SELECT o FROM Opcao o ORDER BY o.ordem"),
    @NamedQuery(name = "Opcao.findById", query = "SELECT o FROM Opcao o WHERE o.id = :id"),
    @NamedQuery(name = "Opcao.findByEnquete", query = "SELECT o FROM Opcao o WHERE o.enquete.id = :enquete ORDER BY o.ordem")})
public class Opcao implements Serializable, Comparable<Opcao> {

    private static final long serialVersionUID = 1L;
    
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Basic(optional = false)
    @Column(name = "ID")
    private Integer id;

    @Basic(optional = false)
    @Column(name = "DESCRICAO")
    private String descricao;

    @JoinColumn(name = "ENQUETE", referencedColumnName = "ID")
    @ManyToOne(optional = false)
    private Enquete enquete;

    @Column(name = "ORDEM")
    private Integer ordem;

    @OneToMany(cascade = CascadeType.ALL, mappedBy = "opcao")
    private List<Voto> votos;

    public Opcao() {
    }

    public Opcao(Integer id) {
        this.id = id;
    }

    public Opcao(Integer id, String descricao) {
        this.id = id;
        this.descricao = descricao;
    }

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public Enquete getEnquete() {
        return enquete;
    }

    public void setEnquete(Enquete id) {
        this.enquete = id;
    }

    public String getDescricao() {
        return descricao;
    }

    public void setDescricao(String descricao) {
        this.descricao = descricao;
    }

    public Integer getOrdem() {
        return ordem;
    }

    public void setOrdem(Integer ordem) {
        this.ordem = ordem;
    }

    public List<Voto> getVotos() {
        return votos;
    }

    public void setVotos(List<Voto> votoList) {
        this.votos = votoList;
    }

    public Integer getQuantidadeVotos() {
        return votos == null ? 0 : votos.size();
    }

    @Override
    public int hashCode() {
        int hash = 0;
        hash += (id != null ? id.hashCode() : 0);
        return hash;
    }

    @Override
    public boolean equals(Object object) {
        if (!(object instanceof Opcao)) {
            return false;
        }
        Opcao other = (Opcao) object;
        if ((this.id == null && other.id != null) || (this.id != null && !this.id.equals(other.id))) {
            return false;
        }
        return true;
    }

    @Override
    public String toString() {
        return "org.puc.enquete.bean.Opcao[id=" + id + "]";
    }

    public int compareTo(Opcao o) {

        if (!(o instanceof Opcao)) {
            throw new RuntimeException("Tipo incompatível");
        }
        return this.getOrdem().compareTo(o.getOrdem());

    }

}
